package com.ce.pms.house.dao.impl;


import java.util.List;

import org.hibernate.Query;

import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.house.dao.IFlatDao;
import com.ce.pms.house.model.CoalitionHouseDomain;
import com.ce.pms.house.model.FlatDomain;
import com.ce.pms.house.model.HouseDomain;
import com.ce.util.BlankUtil;

public class FlatDaoImpl extends HouseDaoImpl implements IFlatDao{

	public CoalitionHouseDomain getCoalition(HouseDomain houseDomain) {
		return super.getCoalition(houseDomain);
	}

	/**
	 * 功能描述:查询指定的普通住宅<br>
	 * @param keyword
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-8<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public FlatDomain getFlatDomain(Long keyword){
		String hql="FROM FlatDomain flat where flat.id=?";
		Query query=createQuery(hql, keyword);
		return (FlatDomain)query.uniqueResult();
	}

	/**
	 * 方法描述: 根据建设期ID和房号Number查询房间ID
	 * @author 胡煦
	 * Create on:2008-12-8
	 * @param projectId，houseNumber
	 * @return List<Object[]>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Object[] getFlatObjsByProjectAndNumber(Long projectId,String houseNumber){
		StringBuffer stringBuffer = new StringBuffer("select housedomai0_.HOUSE_ID as PRODUCT1_78_, housedomai0_.BUILDING_ID as BUILDING8_79_, housedomai0_.HOUSE_NUMBER as HOUSE3_79_, case when housedomai0_2_.MANAGE_ID is not null then 2 when housedomai0_3_.FLAT_ID is not null then 3 when housedomai0_.HOUSE_ID is not null then 1 end as clazz_ from HM_HOUSE housedomai0_ inner join HM_PRODUCT housedomai0_1_ on housedomai0_.HOUSE_ID=housedomai0_1_.PRODUCT_ID left outer join HM_MANAGE housedomai0_2_ on housedomai0_.HOUSE_ID=housedomai0_2_.MANAGE_ID left outer join CS_FLAT housedomai0_3_ on housedomai0_.HOUSE_ID=housedomai0_3_.FLAT_ID, HM_BUILDING buildingdo2_ where housedomai0_.BUILDING_ID=buildingdo2_.BUILDING_ID and buildingdo2_.PROJECT_ID=? and housedomai0_.HOUSE_NUMBER=? and housedomai0_.IS_VIRTUAL=0");
		Query query = this.createSQLQuery(stringBuffer.toString(), new Object[]{projectId,houseNumber});
		Object[] objs = (Object[]) query.uniqueResult();
		return objs;
	}

	public CoalitionHouseDomain getMergeCoalition(HouseDomain houseDomain) {
		CoalitionHouseDomain retValue = getCoalition(houseDomain);
		if(retValue == null) return retValue;
		//双拼(多拼)房  可以合并
		if(SysrefcodeDO.CS_COALITION_HOUSE_RELATION_TYPE_MERGE.equals(retValue.getCoalitionType().getValue())) {
			return retValue;
		}
		//双拼(多拼)与产权共有二者兼之  可以合并
		if(SysrefcodeDO.CS_COALITION_HOUSE_RELATION_TYPE_BILL.equals(retValue.getCoalitionType().getValue())) {
			return retValue;
		}
		return null;
	}

	public CoalitionHouseDomain getShareProRightCoalition(HouseDomain houseDomain) {
		CoalitionHouseDomain retValue = this.getCoalition(houseDomain);

		if (BlankUtil.isBlank(retValue)) {
			return null;
		}

		if(!(SysrefcodeDO.CS_COALITION_HOUSE_RELATION_TYPE_SHARE.equals(retValue.getCoalitionType().getValue())
				|| SysrefcodeDO.CS_COALITION_HOUSE_RELATION_TYPE_BILL.equals(retValue.getCoalitionType().getValue()))) {
			retValue = null;
		}

		return retValue;
	}
	
	/**
	 * 功能描述:查询指定所有ID产权房间列表  <br>
	 * @param houseIds
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<FlatDomain> queryFlatDomainListByIds(String houseIds) {
		String sql = "FROM FlatDomain flat WHERE flat.id IN (:hid)";
		sql = sql.replace(":hid", houseIds);
		Query query = createQuery(sql);
		return query.list();
	}

}
